package com.order.flowserver.config;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import com.shitou.server.data.db.MyBatisMapper;
import com.shitou.server.mybatis.MysqlPageReqParamInterceptor;
import com.shitou.server.mybatis.MysqlPageResultInterceptor;

/**
 * <br>
 *
 * @author 李恒
 * @version 1.0
 * @date: 2016/10/11 0011 下午 5:48
 */
@Configuration
@ConfigurationProperties("jdbc.datasource")
@MapperScan(basePackages = "com.order.flowserver.domain.dataaccess", annotationClass = MyBatisMapper.class)
public class DataSourceConfig {
	// 动态配置从esb config读取
	private String url = "jdbc:mysql://18.16.200.42:3306/test?useUnicode=true&characterEncoding=utf-8";
	private String username = "root";
	private String password = "shitou$root";
	private String driverClassName = "com.mysql.jdbc.Driver";

	public void setUrl(String url) {
		this.url = url;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}

	@Bean
	public DataSource dataSource() {
		DataSource dataSource = new DataSource();
		dataSource.setUrl(url);
		dataSource.setUsername(username);
		dataSource.setPassword(password);
		dataSource.setDriverClassName(driverClassName);
		dataSource.setMaxActive(20);
		dataSource.setMaxIdle(8);
		dataSource.setMinIdle(3);
		dataSource.setInitialSize(1);
		dataSource.setValidationQuery("SELECT 'x' FROM DUAL");
		dataSource.setTestOnBorrow(false);
		dataSource.setCommitOnReturn(false);
		dataSource.setTestWhileIdle(true);
		dataSource.setTimeBetweenEvictionRunsMillis(60000);
		dataSource.setMinEvictableIdleTimeMillis(300000);
		dataSource.setMaxWait(5000);
		return dataSource;
	}

	@Bean
	public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
		final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
		sessionFactory.setDataSource(dataSource);
		Resource[] mappers = (new PathMatchingResourcePatternResolver())
				.getResources("classpath*:mybatis_mapper/*Mapper.xml");
		sessionFactory.setMapperLocations(mappers);
		sessionFactory
				.setPlugins(new Interceptor[] { new MysqlPageReqParamInterceptor(), new MysqlPageResultInterceptor() });
		return sessionFactory.getObject();
	}

	@Bean
	public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("dataSource") DataSource dataSource)
			throws Exception {
		final DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
		dataSourceTransactionManager.setDataSource(dataSource);
		return dataSourceTransactionManager;
	}

	@Bean
	public TransactionTemplate transactionTemplate(
			@Qualifier("dataSourceTransactionManager") DataSourceTransactionManager dataSourceTransactionManager)
					throws Exception {
		final TransactionTemplate jdbcTemplate = new TransactionTemplate();
		jdbcTemplate.setTransactionManager(dataSourceTransactionManager);
		return jdbcTemplate;
	}
}